<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="8" :sm="8">
            <a-form-item label="订单编号">
              <a-input placeholder="请输入订单编号" v-model="queryParam.id"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <a-form-item label="收货人号码">
              <a-input placeholder="请输入收货人号码" v-model="queryParam.receiverPhone"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <a-form-item label="用户编号">
              <a-input placeholder="请输入用户编号" v-model="queryParam.memberId"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <a-form-item label="订单状态">
              <j-multi-select-tag v-model="status_MultiString" placeholder="请选择订单状态" dictCode="order_status"/>
              <!--<j-dict-select-tag v-model="queryParam.status" placeholder="请选择订单状态" dictCode="order_status"/>-->
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="8">
            <a-form-item label="下单时间">
              <a-range-picker format="YYYY-MM-DD HH:mm:ss"
                              style="width:325px;"
                              :show-time="{
                                hideDisabledOptions: true,
                                defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('23:59:59', 'HH:mm:ss')],
                              }"
                              :placeholder="['开始时间', '结束时间']"
                              @change="onCreateTimeChange"/>
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="8">
            <a-form-item label="实付金额">
              <!-- <a-input placeholder="请输入名称查询" v-model="queryParam.age"></a-input>-->
              <a-input placeholder="最小金额" type="ge" v-model="queryParam.payAmount_begin"
                       style="width:calc(50% - 15px);"></a-input>
              <span class="group-query-strig"> ~ </span>
              <a-input placeholder="最大金额" type="le" v-model="queryParam.payAmount_end"
                       style="width:calc(50% - 15px);"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :md="8" :sm="8">
              <a-form-item label="合作方标识">
                <a-input placeholder="请输入合作方标识" v-model="queryParam.netSourceArg4"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-item label="优惠券活动编号">
                <a-input placeholder="请输入优惠券活动编号" v-model="queryParam.couponId_MultiString"></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="8" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="handleExportXls('订单管理')">导出</a-button>
      <a-button type="danger" v-has="'oms:exportByFinance'" icon="download" @click="handleExportXlsForFinance('订单管理')">财务导出</a-button>

      <!--<a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-button @click="handleAdd" type="primary" icon="check-square">订单确认</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>
        <span>已选择</span>
        <a style="font-weight: 600">
          {{ selectedRowKeys.length }}
        </a>
        <span>项</span>
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :scroll="{ x: 2160 }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <span slot="eStatus" slot-scope="text, record">
          <a-switch size="small" :checked="(!(text== null || text === 0))"/>
        </span>

        <span slot="action" slot-scope="text, record">
          <a @click="handleShowDetail(record)">查看</a>
          <a-divider type="vertical"/>
          <a @click="handleTrace(record)">轨迹</a>
          <!--<a @click="handleProcessMemo(record)">跟进</a>-->
          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a href="javascript:;" v-has="'order:editAddress'" @click="handleEditAddress(record)">修改地址</a>
              </a-menu-item>
              <a-menu-item>
                <a href="javascript:;" v-has="'order:editPrice'" @click="handleEditApply(record)">修改价格</a>
              </a-menu-item>
              <a-menu-item>
                <a href="javascript:;" v-has="'order:editFreight'" @click="handleEditFreight(record)">修改运费</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定取消吗?" @confirm="() => handleCancelOrder(record)">
                  <a>取消订单</a>
                </a-popconfirm>
              </a-menu-item>
              <!--<a-menu-item>
                <a href="javascript:;" @click="handleEdit(record)">编辑</a>
              </a-menu-item>-->

              <!--<a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>-->
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <omsOrder-modal ref="modalForm" @ok="modalFormOk"/>

    <oms-order-view-modal ref="OmsOrderViewModal" @ok="modalFormOk"/>

    <oms-order-process-memo-modal ref="OmsOrderProcessMemoModal" @ok="modalFormOk"/>

    <oms-order-edit-address-modal ref="OmsOrderEditAddressModal" @ok="modalFormOk"/>

    <oms-order-edit-apply-modal ref="OmsOrderEditApplyModal" @ok="modalFormOk"/>

    <oms-order-edit-freight-modal ref="OmsOrderEditFreightModal" @ok="modalFormOk"/>

    <oms-order-trace-modal ref="OmsOrderTraceModal" @ok="modalFormOk"/>

  </a-card>
</template>

<script>

  import { httpAction, downFile } from '@/api/manage'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import moment from 'moment'
  import OmsOrderModal from './modules/OmsOrderModal'
  import OmsOrderViewModal from './modules/OmsOrderViewModal'
  import OmsOrderEditAddressModal from './modules/OmsOrderEditAddressModal'
  import OmsOrderEditApplyModal from './modules/OmsOrderEditApplyModal'
  import OmsOrderEditFreightModal from './modules/OmsOrderEditFreightModal'
  import OmsOrderProcessMemoModal from './modules/OmsOrderProcessMemoModal'
  import OmsOrderTraceModal from './modules/OmsOrderTraceModal'
  import JMultiSelectTag from '../../components/dict/JMultiSelectTag'

  export default {
    name: 'OmsOrderList',
    mixins: [JeecgListMixin],
    components: {
      JMultiSelectTag,
      OmsOrderModal,
      OmsOrderViewModal,
      OmsOrderEditAddressModal,
      OmsOrderEditApplyModal,
      OmsOrderEditFreightModal,
      OmsOrderProcessMemoModal,
      OmsOrderTraceModal
    },
    data() {
      return {
        description: '订单管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 50,
            align: 'center',
            customRender: function(t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '门店',
            width: 120,
            align: 'center',
            dataIndex: 'storeId_dictText'
          },
          {
            title: '订单编号',
            width: 160,
            align: 'center',
            dataIndex: 'orderSn'
          },
          {
            title: '订单类型',
            width: 90,
            align: 'center',
            dataIndex: 'orderType_dictText'
          },
          {
            title: '订单状态',
            width: 90,
            align: 'center',
            dataIndex: 'status_dictText'
          },
          {
            title: '货品状态',
            width: 90,
            align: 'center',
            dataIndex: 'productStatus_dictText'
          },
          {
            title: '支付状态',
            width: 90,
            align: 'center',
            dataIndex: 'payStatus_dictText'
          },
          /*{
            title: '挂起状态',
            width: 90,
            align: 'center',
            dataIndex: 'lockStatus',
            scopedSlots: { customRender: 'eStatus' }
          },*/
          {
            title: '下单时间',
            width: 100,
            align: 'center',
            dataIndex: 'createTime'
          },
          {
            title: '支付时间',
            width: 100,
            align: 'center',
            dataIndex: 'paymentTime'
          },
          {
            title: '发货时间',
            width: 100,
            align: 'center',
            dataIndex: 'deliveryTime'
          },
          {
            title: '支付方式',
            width: 80,
            align: 'center',
            dataIndex: 'payType_dictText'
          },
          {
            title: '商品总金额',
            width: 100,
            align: 'center',
            dataIndex: 'totalAmount'
          },
          {
            title: '实付金额',
            width: 90,
            align: 'center',
            dataIndex: 'payAmount'
          },
          {
            title: '运费',
            width: 50,
            align: 'center',
            dataIndex: 'freightAmount'
          },
          {
            title: '促销金额',
            width: 90,
            align: 'center',
            dataIndex: 'promotionAmount'
          },
          {
            title: '合作方标识',
            width: 90,
            align: 'center',
            dataIndex: 'netSourceArg4'
          },
          {
            title: '积分抵扣',
            width: 90,
            align: 'center',
            dataIndex: 'integrationAmount'
          },
          {
            title: '优惠券',
            width: 70,
            align: 'center',
            dataIndex: 'couponAmount'
          },
          {
            title: '红包',
            width: 60,
            align: 'center',
            dataIndex: 'redbagAmount'
          },
          {
            title: '收货人',
            width: 80,
            align: 'center',
            dataIndex: 'receiverName'
          },
          {
            title: '物流公司',
            width: 90,
            align: 'center',
            dataIndex: 'deliveryCompany'
          },
          {
            title: '物流单号',
            width: 90,
            align: 'center',
            dataIndex: 'deliverySn'
          },
          {
            title: '下单系统',
            width: 100,
            align: 'center',
            dataIndex: 'sourceSystem_dictText'
          },
          {
            title: '订单来源',
            width: 100,
            align: 'center',
            dataIndex: 'sourceType'
          },
          {
            title: '退货状态',
            width: 90,
            align: 'center',
            dataIndex: 'returnStatus'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: 160,
            align: 'center',
            fixed: 'right',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 请求参数
        url: {
          list: '/oms/omsOrder/list',
          cancel: '/oms/omsOrder/cancel',
          lockTime: '/oms/omsOrder/editLockTime',
          delete: '/oms/omsOrder/delete',
          deleteBatch: '/oms/omsOrder/deleteBatch',
          exportXlsUrl: 'oms/omsOrder/exportXls',
          importExcelUrl: 'oms/omsOrder/importExcel',
          exportXlsForFinance: 'oms/omsOrder/exportXlsForFinance',
        },
        status_MultiString: '1,2,3'
      }

    },
    computed: {
      importExcelUrl: function() {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
      }
    },
    created() {
      this.queryParam.status_MultiString = '1,2,3'
    },
    watch: {
      status_MultiString(v) {
        this.queryParam.status_MultiString = this.status_MultiString
      }
    },
    methods: {
      moment,
      onStatusChange: function(value) {
        console.log(value)
        this.queryParam.status_MultiString = value
      },
      onCreateTimeChange: function(value, dateString) {
        console.log(dateString[0], dateString[1])
        this.queryParam.createTime_begin = dateString[0]
        this.queryParam.createTime_end = dateString[1]
      },
      handleCancelOrder(record) {
        var that = this
        if (record.status === 0) {
          httpAction(that.url.cancel, { id: record.id }, 'put').then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.loadData();
            } else {
              that.$message.warning(res.message)
            }
          })
        } else {
          that.$message.warning('订单：' + record.id + ' 当前已不可取消')
        }

      },
      handleEditAddress(record) {
        if (record.status === 0 || (record.status === 1 && record.productStatus === 2)) {
          httpAction(this.url.lockTime, { id: record.id }, 'put').then(res => {
            if (res.success) {
              this.$refs.OmsOrderEditAddressModal.show(record)
            } else {
              this.$message.warning('当前订单未能锁定，请刷新后重试')
            }
          })
        } else {
          this.$message.warning('订单：' + record.id + ' 当前已不可修改地址')
        }
      },
      handleShowDetail(record) {
        this.$refs.OmsOrderViewModal.show(record)
      },
      handleProcessMemo(record) {
        this.$refs.OmsOrderProcessMemoModal.show(record)
      },
      handleTrace(record) {
        this.$refs.OmsOrderTraceModal.show(record)
      },
      initDictConfig() {
      },
      handleEditApply(record) {
        if (record.status === 0) {
          this.$refs.OmsOrderEditApplyModal.show(record)
        } else {
          this.$message.warning('订单：' + record.id + ' 当前已不可修改价格')
        }
      },
      handleEditFreight(record) {
        if (record.status === 0) {
          this.$refs.OmsOrderEditFreightModal.show(record)
        } else {
          this.$message.warning('订单：' + record.id + ' 当前已不可修改价格')
        }
      },
      handleExportXlsForFinance(fileName){
        if(!fileName || typeof fileName != "string"){
          fileName = "导出文件"
        }
        let param = {...this.getQueryParams()};
        if(this.selectedRowKeys && this.selectedRowKeys.length>0){
          param['selections'] = this.selectedRowKeys.join(",")
        }
        console.log("导出参数",param)
        downFile(this.url.exportXlsForFinance,param).then((data)=>{
          if (!data) {
            this.$message.warning("文件下载失败")
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName+'.xls')
          }else{
            let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName+'.xls')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link); //下载完成移除元素
            window.URL.revokeObjectURL(url); //释放掉blob对象
          }
        })
      },


    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>